package com.hxkj.client.service.taglib.config;

import com.hxkj.client.service.taglib.directive.DefaultObjectWrapperBuilderFactory;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.*;
import freemarker.core.Environment;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/***
 * @desc cms自定义属性设置环境切面类
 * @author yaosy
 * @date 2018年9月14日18:56:28
 * **/
@Component
@Aspect
@Slf4j
public class DirectiveIntercept {


    @Pointcut("execution(* com.hxkj.client.service.taglib.directive..*(..))")
    public void cmsPointCut() {}


    @Before("cmsPointCut()")
    public void beforePointCut(JoinPoint joinPoint)throws Exception{
        Map<String,Object> enviroment = EnvironmentContextHolder.getEnvironment();
        LocalDateTime localDateTime =  LocalDateTime.now();
        Object[] objects = joinPoint.getArgs();
        for(Object obj :objects){
            if(obj instanceof Environment){
                Environment env = (Environment) obj;
                for (Map.Entry<String, Object> entry : enviroment.entrySet()) {
                    String key =  entry.getKey();
                    Object objValue = entry.getValue();
                    /*if(key.equals("siteUniversal")){
                        if(objValue != null)
                            env.getConfiguration().setSharedVariable("siteUniversal", (SiteUniversal)objValue);
                        continue;
                    }
                    if(key.equals("leaflet")){
                        if(objValue != null)
                            env.getConfiguration().setSharedVariable("leaflet", (Leaflet)objValue);
                        continue;
                    }
                    if(key.equals("column")){
                        if(objValue != null)
                            env.getConfiguration().setSharedVariable("column", (Column)objValue);
                        continue;
                    }
                    if(key.equals("tag")){
                        if(objValue != null)
                            env.getConfiguration().setSharedVariable("tag", (Tags)objValue);
                        continue;
                    }
                    if(key.equals("article")){
                        if(objValue != null)
                            env.getConfiguration().setSharedVariable("article", (Article)objValue);
                        continue;
                    }*/
                    if(objValue != null) {
                        env.setCustomAttribute(key,objValue);
                    }
                }
            }
        }
    }

}
